/*
 * @Author: xianfuyou
 * @Date: 2022-04-01 11:11:57
 * @LastEditors: your name
 * @LastEditTime: 2022-04-11 13:11:40
 * @Description: file content
 */
import React from 'react'
import { Modal, Form, Input, Row, Col, message } from 'antd'
import './css/Model.css'
import Axios from '../../utils/http'
import Qs from 'qs'
import store from '../../redux/store'
import { getOrderListsAction,getAllPriceAction } from '../../redux/action/orderAction/orderAction'


const { TextArea } = Input

export default function ReceivePayModel(props) {
  // const baseUrl = 'http://139.155.237.161:8080'
  const { isShow, pHandleOk, pHandleCancel, nowOrder, requestJson } = props
  const { id, collectMoney, collectMoneyRemarks, orderAmount } = nowOrder
  const [form] = Form.useForm()
  const handleOk = () => {
    const { payMoney, remark } = form.getFieldsValue()
    if (payMoney >= 0 && payMoney <= orderAmount - collectMoney) {
      getPayNow({
        id: id,
        collectMoney: payMoney,
        collectMoneyRemarks: remark
      })
    } else if (payMoney < 0) {
      message.error('金额不能为负！')
    } else {
      message.error('不能大于现有欠款！')
    }
  };

  const handleCancel = () => {
    pHandleCancel(false);
  };

  // 调用到款接口
  const getPayNow = (json) => {
    Axios.post('/order/updateCollectMoneyById', Qs.stringify(json),
      { headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(res => {
        console.log(res)
        store.dispatch(getOrderListsAction(requestJson))
        store.dispatch(getAllPriceAction())
      }).catch(err => {
        console.log(err)
      })
    pHandleOk(false);
  }

  return (
    <div>
      <Modal
        className="xfy_dep_add_modal del_order"
        title="订单到款"
        okText="保存"
        cancelText="关闭"
        onOk={handleOk}
        onCancel={handleCancel}
        afterClose={() => { form.resetFields() }}
        visible={isShow}
        destroyOnClose={true}
        forceRender={true}
      >
        <Form preserve={false} form={form}>
          <Row>
            <Col span={24} className={'title'}>
              新增到款
            </Col>
            <Col span={24}>
              <Form.Item labelCol={{ span: 5 }} label={"到款金额"} name="payMoney">
                <span> <Input style={{ width: '150px' }}></Input>元</span>
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item labelCol={{ span: 5 }} label={"备注"} name="remark">
                <TextArea rows={4}>
                </TextArea>
              </Form.Item>
            </Col>
          </Row>
        </Form>
      </Modal>
    </div>
  )
}
